import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_devices/routes/index.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/ui/max_line_wrap.dart';
import 'package:smart_devices/ui/scroll_refresh_config.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/utils/tools.dart';

import 'task_logic.dart';

class UpKeepTaskView extends GetView<UpkeepTaskLogic> {
  const UpKeepTaskView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
            icon: const Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
              size: 15,
            ),
            onPressed: Get.back),
        automaticallyImplyLeading: false,
        title: Text(
          '保养任务',
          style: StylesUtils.customTextStyle(
              fontSize: 18, fontWeight: FontWeight.w700),
        ),
        centerTitle: true,
        bottom: PreferredSize(
            preferredSize: const Size.fromHeight(50),
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 7),
              decoration: const BoxDecoration(color: Colors.white),
              child: Row(
                children: [
                  Expanded(
                      child: Container(
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(5),
                        border: Border.all(
                            color: ColorsUtils.parseColorFromHexString(
                                ColorsUtils.colorE4e4))),
                    child: Row(
                      children: [
                        Container(
                          width: 36,
                          alignment: Alignment.center,
                          child: ToolsUtils.urlSvg(
                              'https://cdn7.axureshop.com/demo2023/2163241/images/常用组件/u2227.svg'),
                        ),
                        Expanded(
                            child: Obx(() => TextField(
                                  textDirection: TextDirection.ltr,
                                  controller: TextEditingController(
                                      text: controller.search.value),
                                  cursorColor:
                                      ColorsUtils.parseColorFromHexString(
                                          ColorsUtils.color1c24),
                                  style: StylesUtils.customTextStyle(
                                      fontSize: 16,
                                      color: ColorsUtils.color1c24),
                                  textAlign: TextAlign.left,
                                  textAlignVertical: TextAlignVertical.center,
                                  cursorWidth: 2,
                                  // cursorHeight: 18,
                                  cursorRadius: const Radius.circular(2),
                                  decoration: InputDecoration(
                                      hintText: '请输入工单编码/工单主题/备件名称',
                                      hintStyle: StylesUtils.customTextStyle(
                                          color: ColorsUtils.colorCccc,
                                          fontSize: 13),
                                      isDense: true,
                                      isCollapsed: true,
                                      border: InputBorder.none,
                                      focusColor:
                                          ColorsUtils.parseColorFromHexString(
                                              ColorsUtils.color1c24),
                                      contentPadding:
                                          const EdgeInsets.symmetric(
                                              vertical: 13),
                                      focusedBorder: InputBorder.none),
                                  onChanged: (String text) {
                                    controller.search.value = text;
                                  },
                                )))
                      ],
                    ),
                  )),
                  InkWell(
                    onTap: () {
                      // Get.toNamed(Routes.deviceSpareScreen);
                    },
                    child: Container(
                      width: 60,
                      alignment: Alignment.centerRight,
                      child: Text.rich(TextSpan(children: [
                        WidgetSpan(
                            alignment: PlaceholderAlignment.middle,
                            child: ToolsUtils.urlSvg(
                                'https://cdn7.axureshop.com/demo2023/2163241/images/常用组件/u2222.svg',
                                width: 15,
                                height: 15)),
                        TextSpan(
                            text: '刷选',
                            style: StylesUtils.customTextStyle(
                                color: ColorsUtils.color3333))
                      ])),
                    ),
                  )
                ],
              ),
            )),
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  Widget listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext c, int index) {
        return DrawUnderLine(
            child: InkWell(
          onTap: () {
            Get.toNamed(Routes.deviceUpkeepInfo, arguments: {
              'task': controller.pageList[index],
              'details': controller.details.value,
            });
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 15),
            margin:
                index == 0 ? const EdgeInsets.only(top: 15) : EdgeInsets.zero,
            decoration: const BoxDecoration(color: Colors.white),
            child: Column(
              children: [
                Container(
                  height: 40,
                  alignment: Alignment.centerLeft,
                  child: Text(
                    controller.pageList[index].title,
                    style: StylesUtils.customTextStyle(
                        color: ColorsUtils.color0052,
                        fontSize: 16,
                        fontWeight: FontWeight.w700),
                  ),
                ),
                Container(
                  height: 30,
                  alignment: Alignment.centerLeft,
                  child: MaxLineWrap(
                    maxLine: 1,
                    itemAlignment: MaxLineWrapAlignment.start,
                    children: [
                      Text(
                        '任务编号',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color3333, fontSize: 13),
                      ),
                      Text(
                        controller.pageList[index].taskNo,
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.colorAaaa, fontSize: 13),
                      )
                    ],
                  ),
                ),
                Container(
                  height: 30,
                  alignment: Alignment.centerLeft,
                  child: MaxLineWrap(
                    maxLine: 1,
                    itemAlignment: MaxLineWrapAlignment.start,
                    children: [
                      Text(
                        '设备名称',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color3333, fontSize: 13),
                      ),
                      Text(
                        controller.pageList[index].name,
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.colorAaaa, fontSize: 13),
                      )
                    ],
                  ),
                ),
                Container(
                  height: 30,
                  alignment: Alignment.centerLeft,
                  child: MaxLineWrap(
                    maxLine: 1,
                    itemAlignment: MaxLineWrapAlignment.start,
                    children: [
                      Text(
                        '设备编号',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color3333, fontSize: 13),
                      ),
                      Text(
                        controller.pageList[index].code,
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.colorAaaa, fontSize: 13),
                      )
                    ],
                  ),
                ),
                Container(
                  height: 30,
                  alignment: Alignment.centerLeft,
                  child: MaxLineWrap(
                    maxLine: 1,
                    itemAlignment: MaxLineWrapAlignment.start,
                    children: [
                      Text(
                        '存放位置',
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.color3333, fontSize: 13),
                      ),
                      Text(
                        controller.pageList[index].location,
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                        style: StylesUtils.customTextStyle(
                            color: ColorsUtils.colorAaaa, fontSize: 13),
                      )
                    ],
                  ),
                ),
                SizedBox(
                  height: 40,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        controller.pageList[index].addTime,
                        style: StylesUtils.customTextStyle(
                            fontSize: 13, color: ColorsUtils.color3333),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ));
      },
      itemCount: controller.pageList.length,
      itemExtent: 250,
    );
  }
}
